/**
 */

package com.bei_en.teacher.admin.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * @author Guocg
 *
 */
public abstract class BaseService {
	@Resource 
	protected	PlatformTransactionManager platformTransactionManager;   
	protected Logger log = LoggerFactory.getLogger(this.getClass());
	/**
	 * 开始事务
	 * @return
	 */
	public TransactionStatus getTransactionStatus(){
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

		return platformTransactionManager.getTransaction(def);
	}
	/**
	 * 失败的
	 * @param Status
	 */
	public void rollback(TransactionStatus Status){
		platformTransactionManager.rollback(Status);
	}
	/**
	 * 成功的
	 * @param Status
	 */
	public void commit(TransactionStatus Status){
		platformTransactionManager.commit(Status);
	}
	
//	private   org.springframework.transaction.PlatformTransactionManager txManager;    
//	private  ApplicationContext ctx;  
//	public TransactionStatus geTransactionStatus(){
//		if(ctx==null||txManager==null){
//			  String[] configLocations = new String[] {  
//		                "classpath:spring/spring-mybatis.xml"};  
//		        ctx = new ClassPathXmlApplicationContext(configLocations);  
//		        txManager = ctx.getBean(PlatformTransactionManager.class);  
//		}
//		
//		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
//    	def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
//    	TransactionStatus status = txManager.getTransaction(def);
//    	return status;
//	}
//	public PlatformTransactionManager geTransaction(){
//	
//    	return txManager;
//	}
	

}
